package com.erpanderp.wam.cm.view;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.swing.SwingWorker;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.context.RequestContext;

import com.erpanderp.wam.cm.model.entity.AdditionalSkill;
import com.erpanderp.wam.cm.model.entity.EmployeeSkillMatrix;
import com.erpanderp.wam.cm.model.entity.SAPCertification;
import com.erpanderp.wam.cm.model.service.EmployeeSkillDAO;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.WAMHostManager;

@ViewScoped
@ManagedBean
public class ViewEmployeeSkillsBean implements Serializable {

	private static final long serialVersionUID = 1L;
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	
	private WAMUser wamUser;
	private String selectedButton = CommonLookup.VIEW_UNDER_COMPETENCY;
	private boolean skillsListRender;
	private EmailUtil emailUtil;
	private String EMAIL_MESSAGE;
	
	private List<EmployeeSkillMatrix> competencySkilsList = new ArrayList<EmployeeSkillMatrix>();
	private List<EmployeeSkillMatrix> allEmpSkilsList = new ArrayList<EmployeeSkillMatrix>();
	private List<SAPCertification> sapCertifications = new ArrayList<SAPCertification>();
	private List<AdditionalSkill> additionalSAPSkills = new ArrayList<AdditionalSkill>();
	
	private EmployeeSkillDAO employeeSkillDAO =  new EmployeeSkillDAO();
	private EmployeeDAO employeeDAO=new EmployeeDAO();
	
	private EmployeeSkillMatrix employeeSkillMatrix = new EmployeeSkillMatrix();
	private Employee selectedEmployee;

	private int compSno=1; 
	private int allSno=1;
	
	public ViewEmployeeSkillsBean() {
		wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
		
		allEmpSkilsList = employeeSkillDAO.getAllEmployeeSkillsList();
		for(EmployeeSkillMatrix employeeSkillMatrix : allEmpSkilsList){
			employeeSkillMatrix.setsNo(allSno);
			allSno++;
		}
		competencySkilsList = employeeSkillDAO.getAllEmployeeSkillsByCompetency(wamUser.getEmployee().getEmployeeCode());
		for(EmployeeSkillMatrix employeeSkillMatrix : competencySkilsList){
			employeeSkillMatrix.setsNo(compSno);
			compSno++;
		}
		
		setSkillsListRender(true);		
	}
	
   public void renderEmailConfirmDialog(ActionEvent e){
		
		Long employeeId  = (Long) e.getComponent().getAttributes().get("employeeId");
		selectedEmployee = employeeDAO.getEmployeeByEmployeeId(employeeId);
		System.out.println(selectedEmployee);
		RequestContext.getCurrentInstance().addPartialUpdateTarget(":timesheetForm:create_timesheet");		
	}
   
   //Send Email to candidate 
   public String sendMail(ActionEvent e){
		System.out.println("Emai to employee...");
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formate= new SimpleDateFormat("dd-MMM-yyyy ");
		String toDay = formate.format(currentDate.getTime());
		
		Map<String, String> hostsMap = WAMHostManager.getWAMHosts(context);
		String domainName = context.getInitParameter("domainName");
		
		emailUtil = new EmailUtil();
		 if(hostsMap.get(CommonLookup.DEV_EMAIL) == null){
				String[] to = {selectedEmployee.getEmail()};
				emailUtil.setRecieverAddresses(to);
			}else{
				System.out.println("Email: " + hostsMap.get(CommonLookup.DEV_EMAIL));
				String[] to = {hostsMap.get(CommonLookup.DEV_EMAIL)};
				System.out.println(to);
				emailUtil.setRecieverAddresses(to);
			} 
		
		 Calendar cal = Calendar.getInstance();
		 java.util.Date d = new java.util.Date(cal.getTimeInMillis());
		String messageString = "Dear " + selectedEmployee.getFirstName() + " " + selectedEmployee.getLastName() + "" +
								",<br /><br />" +
								"<b>Please update your skill matrix as on date "+toDay+" if any new/changes in your profile.</b><br /><br />" +
														
						        
								"Thanks & Regards, <br />"+CommonLookup.WAM_DEVTEAM_SIGNATURE;;
		
	
		emailUtil.setMessage(messageString);
		//emailUtil.setRecieverAddresses(to);
		emailUtil.setSenderEmail("podquick@erpanderp.com", "WAM - Dev Team");
		emailUtil.setSubject("WAM - Monthly Activity - Update your skill matrix for the month " + new SimpleDateFormat("MMMM").format(d) );

		 FacesContext.getCurrentInstance().addMessage("", new FacesMessage(FacesMessage.SEVERITY_INFO, "you have send email successfully.", ""));
		
		SwingWorker worker = new SwingWorker<String, Object>() {
			@Override
			protected String doInBackground() throws Exception {
				EMAIL_MESSAGE = emailUtil.processAndSendEmail();
				return null;
			}

			@Override
			protected void done() {
					System.out.println("DONE " + EMAIL_MESSAGE);		
			}
		};
		worker.execute();
		System.out.println( "BEFORE RETURNING: " + EMAIL_MESSAGE);
		return EMAIL_MESSAGE;
		
	}

	//BUtton Listener
	public void buttonChangeListner(ActionEvent e){
	     selectedButton = (((MenuItem)e.getComponent()).getValue().toString());	
		 setSkillsListRender(true);	
	}
	//VIEW OR EDIT ACTION
	public void viewOrEditSkillMatrix(){
		System.out.println("=======tremployeeSkillMatrix========="+employeeSkillMatrix.getEmployeeSkillMatrixId());
		sapCertifications = employeeSkillDAO.getAllSAPCertificationsBySkillMatrixID(employeeSkillMatrix.getEmployeeSkillMatrixId());
		additionalSAPSkills = employeeSkillDAO.getAllAdditionalSkillsBySkillMatrixID(employeeSkillMatrix.getEmployeeSkillMatrixId());
		System.out.println("============additionalSAPSkills==========="+additionalSAPSkills.size());
	}
	//CLOSE DIALOG BOX
	public void dialogClose(ActionEvent e){
		//travelRequests.set(travelRequests.indexOf(travelRequest), travelRequestDAO.getTravelRequestById(travelRequest.getTravelRequestId()));
		RequestContext.getCurrentInstance().execute("dialog.hide()");		
	}
	
	//SETTERS AND GETTERS
	public String getSelectedButton() {
		return selectedButton;
	}

	public void setSelectedButton(String selectedButton) {
		this.selectedButton = selectedButton;
	}


	public WAMUser getWamUser() {
		return wamUser;
	}


	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}
	public boolean isSkillsListRender() {
		return skillsListRender;
	}
	public void setSkillsListRender(boolean skillsListRender) {
		this.skillsListRender = skillsListRender;
	}
	public List<EmployeeSkillMatrix> getCompetencySkilsList() {
		return competencySkilsList;
	}
	public void setCompetencySkilsList(List<EmployeeSkillMatrix> competencySkilsList) {
		this.competencySkilsList = competencySkilsList;
	}
	public List<EmployeeSkillMatrix> getAllEmpSkilsList() {
		return allEmpSkilsList;
	}
	public void setAllEmpSkilsList(List<EmployeeSkillMatrix> allEmpSkilsList) {
		this.allEmpSkilsList = allEmpSkilsList;
	}
	public EmployeeSkillDAO getEmployeeSkillDAO() {
		return employeeSkillDAO;
	}
	public void setEmployeeSkillDAO(EmployeeSkillDAO employeeSkillDAO) {
		this.employeeSkillDAO = employeeSkillDAO;
	}
	public EmployeeSkillMatrix getEmployeeSkillMatrix() {
		return employeeSkillMatrix;
	}
	public void setEmployeeSkillMatrix(EmployeeSkillMatrix employeeSkillMatrix) {
		this.employeeSkillMatrix = employeeSkillMatrix;
	}
	public List<SAPCertification> getSapCertifications() {
		return sapCertifications;
	}
	public void setSapCertifications(List<SAPCertification> sapCertifications) {
		this.sapCertifications = sapCertifications;
	}
	public List<AdditionalSkill> getAdditionalSAPSkills() {
		return additionalSAPSkills;
	}
	public void setAdditionalSAPSkills(List<AdditionalSkill> additionalSAPSkills) {
		this.additionalSAPSkills = additionalSAPSkills;
	}
	public int getCompSno() {
		return compSno;
	}
	public void setCompSno(int compSno) {
		this.compSno = compSno;
	}
	public int getAllSno() {
		return allSno;
	}
	public void setAllSno(int allSno) {
		this.allSno = allSno;
	}

	public Employee getSelectedEmployee() {
		return selectedEmployee;
	}

	public void setSelectedEmployee(Employee selectedEmployee) {
		this.selectedEmployee = selectedEmployee;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public EmailUtil getEmailUtil() {
		return emailUtil;
	}

	public void setEmailUtil(EmailUtil emailUtil) {
		this.emailUtil = emailUtil;
	}

	public String getEMAIL_MESSAGE() {
		return EMAIL_MESSAGE;
	}

	public void setEMAIL_MESSAGE(String eMAIL_MESSAGE) {
		EMAIL_MESSAGE = eMAIL_MESSAGE;
	}

}
